﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Text;
using web.bussines;
using web.data.entities;
using web.core.Models;
using web.core.ViewModels;
using web.component;


namespace web.core.Controllers
{
    public class shopController : baseController
    {
        //page list product
        public ActionResult product()
        {
            return PartialView("../shop/product");
        }

        //page product detail
        public ActionResult detail()
        {
            return PartialView("../shop/detail");
        }

        #region product_search
        //page list product
        public ActionResult product_search_basic()
        {
            return PartialView("../shop/product_search");
        }

        [ChildActionOnly]
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult ListProductSearchResult()
        {
            string search =Request.QueryString["search"]!=null ? Request.QueryString["search"].ToString():"";
            int? languageId = languageModels.GetLanguageIDbyUrl(Request.Url.PathAndQuery);
            var page_view = new ViewModels.PageListProductViewModels();
            return ListProductSearchResult(languageId, search, null, null, "product_name", "desc", "grid");
        }

        [ChildActionOnly]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ListProductSearchResult(int? languageId, string search, int? page, int? pageSize, string orderBy, string orderType, string showStyle)
        {
            var alias_cate_str = "";
            var name_cate_str = "";
            var img_cate_str = "";
            page = Request.QueryString["page"] != null ? int.Parse(Request.QueryString["page"].ToString()) : 1;
            
            var total = 0;
            List<SelectListItem> sizeDrop = GetSizePagingPublic(pageSize ?? 10);

            List<SelectListItem> orderDrop = new List<SelectListItem>();
            orderDrop.AddRange(new SelectListItem[] { 
                            new SelectListItem{Selected=orderBy=="product_name"?true:false, Text = "Tên", Value = "product_name" },
                            new SelectListItem{Selected=orderBy=="price"?true:false, Text = "Giá", Value = "price" },
                            new SelectListItem{Selected=orderBy=="created_date"?true:false, Text = "Cập nhật", Value = "created_date" }
                        });

            if (string.IsNullOrEmpty(showStyle))
            {
                showStyle = "grid";                 //default style is grid
            }
            ViewBag.style = showStyle;
            
            if (string.IsNullOrEmpty(orderBy))
            {
                ViewBag.orderBy = "product_name"; //default order by product_name
                orderBy = "product_name";
            }
            if (string.IsNullOrEmpty(orderType))
            {
                ViewBag.orderType = "desc"; //default sort by desc
                orderType = "desc";
            }

            var page_view = new ViewModels.PageListProductViewModels();
            page = page ?? 1;
            page_view.list_pagesize = sizeDrop;
            page_view.pagesize = pageSize ?? 10;
            page_view.list_order = orderDrop;
            page_view.orderBy = orderBy;
            page_view.orderType = orderType;
            page_view.showStyle = showStyle;
            page_view.page_list_product = Models.productModels.GetListProductSearch(languageId, search, (page ?? 1), (pageSize ?? 10), orderBy, orderType, out total);
            page_view.total_recod = total;
            page_view.alias_cate = alias_cate_str;
            page_view.alias_name = string.Format("Tìm kiếm : '{0}'", search);//name_cate_str;
            page_view.img_cate = img_cate_str;
            page_view.text_search = search;
            page_view.controller = ControllerName; //"shop";
            page_view.parent_action = ParentActionName; //"product_search_basic";
            return PartialView("../Shared/product/product_list", page_view);
        }
        #endregion

        #region ListProduct
        [ChildActionOnly]
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult ListProduct(int? cateId, string showStyle)
        {
            int? languageId = languageModels.GetLanguageIDbyUrl(Request.Url.PathAndQuery);
            int currentPage = 1;
            int pageSize = 10;  //default pagesize = 10
            if (string.IsNullOrEmpty(showStyle)) {
                showStyle = "grid";                 //default style is grid
            }
            ViewBag.style = showStyle;
            ViewBag.orderBy = "product_name"; //default order by product_name
            ViewBag.orderType = "desc"; //default sort by desc
            return ListProduct(languageId, cateId, currentPage, pageSize, "product_name", "desc", showStyle);
        }

        [ChildActionOnly]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ListProduct(int? languageId,int? cateId, int? page, int? pageSize, string orderBy, string orderType, string showStyle)
        {
            var alias_cate_str = "";
            var name_cate_str = "";
            var img_cate_str = "";
            page = Request.QueryString["page"] != null ? int.Parse(Request.QueryString["page"].ToString()) : 1;
            if (cateId != null)
            {
                var category = productModels.GetProductCategoryViewByID((int)cateId, languageId);
                if (category != null)
                {
                    alias_cate_str = string.IsNullOrEmpty(category.category_alias) != true ? category.category_alias : "san_pham";
                    name_cate_str = string.IsNullOrEmpty(category.category_name) != true ? category.category_name : "Sản phẩm";
                    img_cate_str = string.IsNullOrEmpty(category.images) != true ? category.images : string.Empty;
                }
                else {
                    alias_cate_str = "san_pham";
                    name_cate_str = "Sản phẩm";
                    img_cate_str = string.Empty;
                }
                
            }
            else
            {
                alias_cate_str = "san_pham";
                name_cate_str = "Sản phẩm";
                img_cate_str = string.Empty;
            }
            var total = 0;
            List<SelectListItem> sizeDrop = GetSizePagingPublic(pageSize??10);

            List<SelectListItem> orderDrop = new List<SelectListItem>();
            orderDrop.AddRange(new SelectListItem[] { 
                            new SelectListItem{Selected=orderBy=="product_name"?true:false, Text = "Tên", Value = "product_name" },
                            new SelectListItem{Selected=orderBy=="price"?true:false, Text = "Giá", Value = "price" },
                            new SelectListItem{Selected=orderBy=="created_date"?true:false, Text = "Cập nhật", Value = "created_date" }
                        }); 

            var page_view = new ViewModels.PageListProductViewModels();
            page = page ?? 1;
            page_view.list_pagesize = sizeDrop;
            page_view.pagesize = pageSize ?? 10;
            page_view.list_order = orderDrop;
            page_view.orderBy = orderBy;
            page_view.orderType = orderType;
            page_view.showStyle = showStyle;
            page_view.page_list_product = Models.productModels.GetListProductByCategoryID(languageId, cateId, (page ?? 1), (pageSize ?? 10), orderBy, orderType, out total);
            page_view.total_recod = total;
            page_view.alias_cate = alias_cate_str;
            page_view.alias_name = name_cate_str;
            page_view.img_cate = img_cate_str;
            page_view.controller = ControllerName; //"shop";
            page_view.parent_action = ParentActionName;// "product";
            return PartialView("../Shared/product/product_list", page_view);
        }

        /// <summary>
        /// Get metadata for product list
        /// </summary>
        /// <param name="cateId"></param>
        /// <returns></returns>
        [ChildActionOnly]
        public ActionResult product_metadata(int? cateId)
        {
            int? languageId = languageModels.GetLanguageIDbyUrl(Request.Url.PathAndQuery);
            var cate = productModels.GetProductCategoryViewByID(cateId??0,languageId);
            var metadata_view = new ViewModels.MetadataViewModels();
            if (cate != null)
            {
                metadata_view.title = Ucommon.GetDefaultString(cate.category_title, Ucommon.GetDefaultString(cate.category_name, "Sản phẩm"));
                metadata_view.keyword = Ucommon.GetDefaultString(cate.category_meta_keyword, "");
                metadata_view.description = Ucommon.GetDefaultString(cate.category_meta_description, "");
            }
            base_metadata(metadata_view);
            return PartialView("../Shared/commont/metadata");
        }

        #endregion 

        #region ProductDetail
        [ChildActionOnly]
        public ActionResult ProductDetail(int? id, string alias_cate)
        {
            int? languageId = languageModels.GetLanguageIDbyUrl(Request.Url.PathAndQuery);
            var alias_cate_str = "";
            if (!string.IsNullOrEmpty(alias_cate))
            {
                alias_cate_str = alias_cate;
            }
            else
            {
                alias_cate_str = "san_pham";
            }
            var numberImg = 5;
            var numberProductRelate = 10;
            var numberProductOther = 10;
            var product_view = new ViewModels.ProductViewModels();
            var v_product = productModels.GetProductViewByID(id ?? 0, Request.Url.PathAndQuery);
            if (v_product != null)
            {
                product_view.product_id = v_product.product_id;
                product_view.product_code = v_product.product_code;
                product_view.product_status = v_product.product_status;
                product_view.created_date = v_product.created_date;
                product_view.modify_date = v_product.modify_date;
                product_view.modify_by = v_product.modify_by;
                product_view.quantity = v_product.quantity;
                product_view.price = v_product.price;
                product_view.old_price = v_product.old_price;
                product_view.images = v_product.images;
                product_view.tax = v_product.tax;
                product_view.sales = v_product.sales;
                product_view.product_item_id = v_product.product_item_id;
                product_view.product_name = v_product.product_name;
                product_view.product_alias = v_product.product_alias;
                product_view.product_desc_short = v_product.product_desc_short;
                product_view.product_desc = v_product.product_desc;
                product_view.product_features = v_product.product_features;
                product_view.product_title = v_product.product_title;
                product_view.product_meta_keyword = v_product.product_meta_keyword;
                product_view.product_meta_description = v_product.product_meta_description;
                product_view.product_item_enable = v_product.product_item_enable;
                product_view.language_id = v_product.language_id;

                product_view.list_images = productModels.GetListImagesByProduct(id ?? 0, numberImg);
                product_view.list_product_relate = productModels.GetListProductRelated(id ?? 0,numberProductRelate);
                product_view.list_product_tag = productModels.GetListProductTagByProductID(id ?? 0);
                product_view.list_product_other_in_category = productModels.ListOtherInCategory(languageId, v_product.category_id, (id ?? 0), numberProductOther);
            }

            return PartialView("../Shared/product/product_detail", product_view);
        }

        /// <summary>
        /// Get metadata for product detail
        /// </summary>
        /// <param name="cateId"></param>
        /// <returns></returns>
        [ChildActionOnly]
        public ActionResult product_detail_metadata(int? id)
        {
            var product = productModels.GetProductViewByID(id ?? 0,Request.Url.PathAndQuery);
            var metadata_view = new ViewModels.MetadataViewModels();
            if (product != null)
            {
                metadata_view.title = Ucommon.GetDefaultString(product.product_title, Ucommon.GetDefaultString(product.product_name, ""));
                metadata_view.keyword = Ucommon.GetDefaultString(product.product_meta_keyword, "");
                metadata_view.description = Ucommon.GetDefaultString(product.product_meta_description, "");
            }
            base_metadata(metadata_view);
            return PartialView("../Shared/commont/metadata");
        }

        #endregion 

        #region ListProductSpecial : hot, new
        [ChildActionOnly]
        public ActionResult ListProductSpecial(string cate_param, int? number_items,string show_type)
        {
            int? languageId = languageModels.GetLanguageIDbyUrl(Request.Url.PathAndQuery);
            var name_cate_str = "";
            var partial_view = "";
            if (!string.IsNullOrEmpty(cate_param))
            {
                if (int.Parse(cate_param) == (int)web.component.global_const.E_PRODUCT_PARAM.HOT)
                {
                    if (show_type == web.component.global_const.E_VECTER_HORIZONTAL)
                    {
                        partial_view = "../Shared/home/product_hot";
                    }
                    else if (show_type == web.component.global_const.E_VECTER_VERTICAL)
                    {
                        partial_view = "";
                    }                    
                }
                else if (int.Parse(cate_param) == (int)web.component.global_const.E_PRODUCT_PARAM.NEW)
                {
                    if (show_type == web.component.global_const.E_VECTER_HORIZONTAL)
                    {
                        partial_view = "../Shared/home/product_new";
                    }
                    else if (show_type == web.component.global_const.E_VECTER_VERTICAL)
                    {
                        partial_view = "";
                    }        
                    
                }
                else if (int.Parse(cate_param) == (int)web.component.global_const.E_PRODUCT_PARAM.SALE)
                {
                    partial_view = "";
                }
                else if (int.Parse(cate_param) == (int)web.component.global_const.E_PRODUCT_PARAM.BEST)
                {
                    partial_view = "";
                }
                else if (int.Parse(cate_param) == (int)web.component.global_const.E_PRODUCT_PARAM.FAVOR)
                {
                    partial_view = "";
                }
                else if (int.Parse(cate_param) == (int)web.component.global_const.E_PRODUCT_PARAM.MOST_VIEW)
                {
                    partial_view = "";
                }
                else
                {
                    cate_param = null;
                }
            }
            else
            {
                //alias_cate_str = "san_pham";
                //name_cate_str = "Sản phẩm";
                //img_cate_str = string.Empty;
            }
            
            var page_view = new ViewModels.ListProductSpecialViewModels();
            page_view.list_product_special = Models.productModels.GetListProductViewByParam(cate_param,languageId,number_items);
            page_view.alias_name = name_cate_str;
            //page_view.img_cate = img_cate_str;
            return PartialView(partial_view, page_view);
        }

        #endregion

        #region page list product by tag
        //page list product by tag
        public ActionResult product_by_tag()
        {
            return PartialView("../shop/product_by_tag");
        }

        [ChildActionOnly]
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult ListProductByTag(string tag, string showStyle)
        {
            int? languageId = languageModels.GetLanguageIDbyUrl(Request.Url.PathAndQuery);
            int currentPage = 1;
            int pageSize = 10;  //default pagesize = 10
            if (string.IsNullOrEmpty(showStyle))
            {
                showStyle = "grid";                 //default style is grid
            }
            ViewBag.style = showStyle;
            ViewBag.orderBy = "product_name"; //default order by product_name
            ViewBag.orderType = "desc"; //default sort by desc
            return ListProductByTag(languageId, tag, currentPage, pageSize, "product_name", "desc", showStyle);
        }

        [ChildActionOnly]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ListProductByTag(int? languageId, string tag, int? page, int? pageSize, string orderBy, string orderType, string showStyle)
        {
            var alias_cate_str = "";
            var name_cate_str = "";
            var img_cate_str = "";
            page = Request.QueryString["page"] != null ? int.Parse(Request.QueryString["page"].ToString()) : 1;

            alias_cate_str = "san_pham";
            name_cate_str = "Sản phẩm";
            img_cate_str = string.Empty;

            if (tag != null)
            {
                var tags = productModels.GetTag(tag);
                if (tags != null)
                {
                    alias_cate_str = Ucommon.GetDefaultString(tags.link, "san_pham");
                    name_cate_str = Ucommon.GetDefaultString(tags.tag, "Sản phẩm");
                }
            }

            var total = 0;
            List<SelectListItem> sizeDrop = GetSizePagingPublic(pageSize ?? 10);

            List<SelectListItem> orderDrop = new List<SelectListItem>();
            orderDrop.AddRange(new SelectListItem[] { 
                            new SelectListItem{Selected=orderBy=="product_name"?true:false, Text = "Tên", Value = "product_name" },
                            new SelectListItem{Selected=orderBy=="price"?true:false, Text = "Giá", Value = "price" },
                            new SelectListItem{Selected=orderBy=="created_date"?true:false, Text = "Cập nhật", Value = "created_date" }
                        });
            page = page ?? 1;
            var page_view = new ViewModels.PageListProductViewModels()
            {
                list_pagesize = sizeDrop,
                pagesize = pageSize ?? 10,
                list_order = orderDrop,
                orderBy = orderBy,
                orderType = orderType,
                showStyle = showStyle,
                page_list_product = Models.productModels.GetListProductByTag(languageId, tag, (page ?? 1), (pageSize ?? 10), orderBy, orderType, out total),
                total_recod = total,
                alias_cate = alias_cate_str,
                alias_name = string.Format("Chủ đề sản phẩm : {0}", name_cate_str),
                img_cate = img_cate_str,
                tag = tag,
                controller = ControllerName,
                parent_action = ParentActionName
            };

            return PartialView("../Shared/product/product_list", page_view);
        }

        /// <summary>
        /// Get metadata for product bay tag
        /// </summary>
        /// <param name="cateId"></param>
        /// <returns></returns>
        [ChildActionOnly]
        public ActionResult product_tag_metadata(string tag)
        {
            int? languageId = languageModels.GetLanguageIDbyUrl(Request.Url.PathAndQuery);
            //var cate = productModels.GetProductCategoryViewByID(cateId ?? 0, languageId);
            var tags = productModels.GetTag(tag);
            var metadata_view = new ViewModels.MetadataViewModels();
            if (tags != null)
            {
                metadata_view.title = string.IsNullOrEmpty(tags.tag) == false ? tags.tag : "";
                metadata_view.keyword = string.IsNullOrEmpty(tags.tag) == false ? tags.tag : "";
                metadata_view.description = string.IsNullOrEmpty(tags.tag) == false ? tags.tag : "";
            }
            base_metadata(metadata_view);
            return PartialView("../Shared/commont/metadata");
        }
        #endregion
    }
}
